<template>
  <div class="register">
    <el-form
      :rules="rules"
      ref="registerForm"
      class="register-container"
      label-position="left"
      :model="registerForm"
      label-width="0px"
      v-loading="loading"
    >
      <h3 class="register_title">用户注册</h3>
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="registerForm.username"
          autocomplete="off"
          placeholder="账号"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="registerForm.password"
          autocomplete="off"
          placeholder="输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="checkPass">
        <el-input
          type="password"
          v-model="registerForm.checkPass"
          autocomplete="off"
          placeholder="确认密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('registerForm')"
          style="width: 100%"
          >注册</el-button
        >
        <a class="login" @click="gotoLogin">已有账号？立即登录</a>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {insertUser} from '@/apis/insertData.js'
export default {
  name: "Register",
  data() {
    //验证密码
    let validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.registerForm.checkPass !== "") {
          this.$refs.registerForm.validateField("checkPass");
        }
        callback();
      }
    };
    //二次验证密码
    let validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致"));
      } else {
        callback();
      }
    };

    return {
      info: null ,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "change" },
        ],
        password: [
          { required: true, validator: validatePass, trigger: "change" },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "change" },
        ],
      },
      checked: true,
      registerForm: {
        username: "",
        password: "",
        checkPass: "",
        
      },
      //loading:false,
    };
  },
  methods: {
    submitForm(registerForm) {
      console.log("方法执行");
      //this.loading = true;
      let params = new FormData();
      params.append("username",this.registerForm.username);
      params.append("password",this.registerForm.password);
      params.append("avatar",null);
      this.$refs[registerForm].validate((valid) => {
        console.log("加入回调");
        if (valid) {
          this.$message({
            type: "success",
            message: "注册成功",
          });
          insertUser(params).then(this.$router.push('/login'));
          
        } else {
          console.log("error submit!!!");
          alert("注册失败！");
          this.loading = false;
          return false;
        }
      });
    },
    gotoLogin() {
      this.$router.push({
        path: "/login",
      });
    },
  },
};
</script>

<style scoped>
.register-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: 180px auto;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
}

.register_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
.register a {
  text-decoration: none;
}

.register a:hover {
  color: cornflowerblue;
}
</style>